/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: ConfigurableCompositeFilter.java 76 2007-01-23 22:18:29Z jack.rybicki $
 */
package logmatcher.filters;

import logmatcher.Configurable;
import logmatcher.InputFilter;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.Arrays;


/**
 * Composite filter which can be initialized from configuration.
 *
 * @version $Revision: 76 $
 */
public class ConfigurableCompositeFilter extends CompositeFilter implements Configurable {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

    /** Class logger. */
    private static final Log LOGGER = LogFactory.getLog(ConfigurableCompositeFilter.class);

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     * Add all configured filters.
     *
     * @see logmatcher.Configurable#configure(org.apache.commons.configuration.Configuration, java.lang.String)
     */
    public void configure(Configuration config, String id) {
        String[] fNames = config.getStringArray(id + ".filters");

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Filters for current collector: " + Arrays.toString(fNames));
        }

        setFilters(new ArrayList<InputFilter>(fNames.length));

        for (int i = 0; i < fNames.length; i++) {
            InputFilter filter = (InputFilter)config.getProperty("logmatcher.filter." + fNames[i]);
            getFilters().add(filter);
        }
    }
}
